package org.future.task.core.handler;

import org.future.task.core.cache.ParamCache;
import org.future.task.core.util.CloneUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HandClient {

    private String queueName;

    public HandClient(String queueName) {
        this.queueName = queueName;
    }

    public static HandClient cli(String queueName) {
        return ParamCache.getHandClient(queueName);
    }

    public void sendData(Object data) throws InterruptedException {
        Connection connection = ParamCache.getConnection(queueName);
        sendData(connection, data);
    }

    public void sendCopyData(Object data) throws InterruptedException {
        Connection connection = ParamCache.getConnection(queueName);
        Object clone = CloneUtils.clone(data);
        if (clone == null) {
            throw new RuntimeException("类复制异常！");
        }
        sendData(connection, clone);
    }

    public void sendData(Connection connection, Object data) throws InterruptedException {
        if (connection == null) {
            connection = ParamCache.getPublicConnection();
        }
        connection.sendData(queueName, data);
    }

    public void sendCopyData(Connection connection, Object data) throws InterruptedException {
        if (connection == null) {
            connection = ParamCache.getPublicConnection();
        }
        Object clone = CloneUtils.clone(data);
        if (clone == null) {
            throw new RuntimeException("类复制异常！");
        }

        connection.sendData(queueName, clone);
    }


    public void sendDataList(List<?> dataList) throws Exception {
        Connection connection = ParamCache.getConnection(queueName);
        sendDataList(connection, dataList);
    }

    public void sendDataList(Connection connection, List<?> dataList) throws Exception {
        if (connection == null) {
            connection = ParamCache.getPublicConnection();
        }
        connection.sendDataList(queueName, dataList);
    }

    public long size() {
        Connection connection = ParamCache.getConnection(this.queueName);
        if (connection == null) {
            connection = ParamCache.getPublicConnection();
        }
        if (connection != null) {
            return connection.size(queueName);
        }
        return 0;
    }

    /**
     * 是否超过限制长度
     *
     * @param limit
     * @return
     */
    public boolean overSize(long limit) {
        if (limit < this.size()) {
            return false;
        }
        return true;
    }

    /**
     * @param unique    唯一标识区分工单是否重复
     * @param sleepTime
     */
    public static void sleep(String unique, long sleepTime) throws InterruptedException {
        long threadId = Thread.currentThread().getId();
        String lastUnique = ParamCache.getLastUnique(String.valueOf(threadId));
        if (lastUnique == null) {
            ParamCache.setLastUnique(String.valueOf(threadId), unique);
            return;
        }
        if (lastUnique.equalsIgnoreCase(unique)) {
            Thread.sleep(sleepTime);
        }
        ParamCache.removeUnique(String.valueOf(threadId));

    }

    public static <T> List<List<T>> splitList(List<T> list, int len) {
        if ((list == null) || (list.isEmpty()) || (len < 1)) {
            return Collections.emptyList();
        }
        List<List<T>> result = new ArrayList<>();

        int size = list.size();
        int count = (size + len - 1) / len;

        for (int i = 0; i < count; ++i) {
            List<T> subList = list.subList(i * len, Math.min((i + 1) * len, size));
            result.add(subList);
        }
        return result;
    }
}
